/**
 * Created by bai on 2015/7/11.
 */

var TakeOut = function(){
    /*
     * 外卖列表
     * */
    var takeOutList = function(fun){


        var options = {};
        options.successFunction = function (data) {
            fun();
            $('.take-out-list').html('');
            $.each(data.data,function(k,v){
                var time=new Date(v.time.sec*1000);
                time=time.getHours()+":"+time.getMinutes();

                $('<dl data-function="btnnotacceptTakeOut" data-id="'+v.id+'">\
                    <div class="listImg" data-function="btnnotacceptTakeOut"></div>\
                        <dt data-function="btnnotacceptTakeOut">美团外卖</dt>\
                        <dd data-function="btnnotacceptTakeOut">地址：'+v.targetLabel+'</dd>\
                        <div class="time" data-function="btnnotacceptTakeOut">'+time+'</div>\
                        <div class="contDot" data-function="btnnotacceptTakeOut"></div>\
                    </dl>').appendTo('.take-out-list');
            })
            var optionsO = {};
            options.start ="";
            options.end ="";
            optionsO.orderType = ['takeout'];
            optionsO.Status = ["new","confirmed","finished","billed"];//"pre","new","confirmed","finished","billed"
            optionsO.successFunction = function (data) {
                $.each(data.data,function(k,v){

                    var time=new Date(v.time.sec*1000);
                    time=time.getHours()+":"+time.getMinutes();

                    $('<dl data-function="btnacceptTakeOut" data-id="'+v.id+'">\
                    <div class="listImg" data-function="btnacceptTakeOut"></div>\
                        <dt data-function="btnacceptTakeOut">外卖</dt>\
                        <dd data-function="btnacceptTakeOut">地址：'+v.targetLabel+'</dd>\
                        <div class="time" data-function="btnacceptTakeOut">'+time+'</div>\
                        <div class="contDot" style="display: none" data-function="btnacceptTakeOut"></div>\
                    </dl>').appendTo('.take-out-list');
                })
            };
            OrderingSystem.getDayOrdertListByStatus(optionsO);
            /*     var optionsT = {};
             optionsT.successFunction = function (data) {
             $.each(data.data,function(k,v){

             var time=new Date(v.time.sec*1000);
             time=time.getHours()+":"+time.getMinutes();

             $('<dl data-function="btnacceptTakeOut" data-id="'+v.id+'">\
             <div class="listImg" data-function="btnacceptTakeOut"></div>\
             <dt data-function="btnacceptTakeOut">美团外卖</dt>\
             <dd data-function="btnacceptTakeOut">地址：'+v.targetLabel+'</dd>\
             <div class="time" data-function="btnacceptTakeOut">'+time+'</div>\
             <div class="contDot" style="display: none" data-function="btnacceptTakeOut"></div>\
             </dl>').appendTo('.take-out-list');
             })
             };
             OrderingSystem.getTakeoutAcceptedOrderList(optionsT);*/
        };
        OrderingSystem.getTakeoutAwaitOrderList(options);
    }
    /*
     * 已接受外卖详情
     * */
    var acceptedTakeOutInfo = function(obj,fun){
        if(obj.parents('dl').data('id')){
            var oId = obj.closest('dl').data('id');
        }else{
            oId=obj.data('id');
        }

        OrderingSystem.getOrderById(oId, function (data){
            fun();
            $('#subteamOrderDetail').html('');
            var dataOfPort=data.data[0];
            var cpList="";
            var token="";
            var addressLong=(document.documentElement.clientWidth-150)+"px";
            if(dataOfPort.tokenList[0]){
                token=dataOfPort.tokenList[0].name;
            }else{
                token="无"
            }
            var request=" ";
            if(dataOfPort.request.length==0){
                request=""
            }else{
                request= dataOfPort.request.specialRequest;
            }

            $.each(data.data[0].itemList,function(k,v){
                cpList+='<dl class="menuList">\
                <p class="dishesName">'+v.name+'</p>\
                <p class="price">￥'+v.price/100+'</p>\
                <p class="count">X'+v.count+'</p>\
                </dl>'
            })
            /*优惠券*/
            var tokenId=[];
            $.each(dataOfPort.tokenList,function(k,v){
                tokenId.push(v.id);
            })
            OrderingSystem.caluAmount(oId, tokenId, function (dataCalu){
                var discountMoney=dataCalu.data[0].caluAmount?(dataCalu.data[0].caluAmount/100):"0";
                $('     <div class="cont">\
             <div class="main">\
                <dl class="orderTitle">\
                    <div class="listImg"></div>\
                    <dt>外卖</dt>\
                </dl>\
                <dl class="personInfo">\
                <div class="listHead">联系方式</div>\
                <pre>姓名：<br>\
电话：'+dataOfPort.mobile+'<br>\
<p>地址：'+dataOfPort.targetLabel+'<p><br>\
            </pre>\
            <a href="tel:'+dataOfPort.mobile+'"></a>\
            </dl>\
            <dl class="menuChose">\
                <div>支付状态</div>\
                <p>货到付款</p>\
                <h6>￥'+discountMoney+'</h6>\
            </dl>\
            <dl class="menuChose">\
                <div>接受状态</div>\
                <p>'+dataOfPort.userName+'接受任务</p>\
                </dl>\
                    <dl class="menuChose">\
                        <div>订单详情：</div>\
                    </dl>\
                    '+cpList+'\
                         <dl class="menuInfo">\
             <div class="listHead">\
                <pre>商品数量：10<br /><br />特殊需求：<span>'+request+'</span>\
</pre>\
                </div>\
                <pre class="menuSum">共计：'+dataOfPort.total/100+'元<br/>' +
                    '优惠券：'+token+'<br/>合计：'+discountMoney+'元<br/>\
                </pre>\
                </dl>\
                </div>\
                </div>').appendTo('#subteamOrderDetail');
            })

        })
    }
    /*
     * 未接受的外卖详情
     * */
    var notAcceptedTakeOutInfo = function(obj,fun){
        if(obj.parents('dl').data('id')){
            var oId = obj.closest('dl').data('id');
        }else{
            oId=obj.data('id');
        }


        OrderingSystem.getOrderById(oId, function (data) {
            var dataOfPort=data.data[0];
            var cpList="";
            var addressLong=(document.documentElement.clientWidth-150)+"px";
            $('#subteamOrderPage').html('');
            var request="";
            if(dataOfPort.request.length==0){

            }else{
                request= dataOfPort.request.specialRequest;
            }
            fun();
            $.each(data.data[0].itemList,function(k,v){
                cpList+=v.name+v.count+"份、";
            })
            $('<div class="cont">\
                <div class="main">\
                <dl class="orderTitle">\
                    <div class="listImg"></div>\
                    <dt>外卖</dt>\
                </dl>\
                <dl class="personInfo">\
                <div class="listHead">联系方式</div>\
                <pre>姓名：<br>\
电话：'+dataOfPort.mobile+'<br>\
<p style="width: '+addressLong+';">地址：'+dataOfPort.takeawayAddress.city+dataOfPort.takeawayAddress.suburb+dataOfPort.takeawayAddress.address+'</p><br>\
            </pre>\
            <a href="tel:'+dataOfPort.mobile+'"></a>\
            </dl>\
            <dl class="severCont">\
                <p class="go" data-function="btn_goToTakeOutOrderInfo" data-id="'+dataOfPort.id+'">〉</p>\
                <div class="sever">服务内容</div>\
                <pre data-function="btn_goToTakeOutOrderInfo" data-id="'+dataOfPort.id+'">外卖<p class="no">券</p><br>\
菜品：'+cpList+'<br>\
特殊需求：<span class="demand">'+request+'</span>\
</pre>\
            </dl>\
            <dl class="menuChose">\
                <div>支付状态</div>\
                <p>货到付款</p>\
                <h6>'+dataOfPort.total/100+'</h6>\
                </dl>\
                <dl class="menuChose">\
                    <div>接受状态</div>\
                    <p>服务未接受</p>\
                </dl>\
                <dl class="menuChose">\
                <div>服务状态</div>\
                <p>服务未确认</p>\
                </dl>\
                </div>\
                </div>').appendTo('#subteamOrderPage')
            $('<div class="bottom">\
                <li>\
                <div class="refuse" data-function="btn_refuseTakeOutTask" data-id="'+dataOfPort.id+'">拒绝任务</div>\
                </li>\
                <li>\
                    <div class="accept" data-function="btn_acceptTakeOutTask"  data-id="'+dataOfPort.id+'">接受任务</div>\
                </li>\
                </div>').appendTo('#subteamOrderPage');
            /*         var timeNew=new Date();
             timeNew=timeNew.getFullYear()+(timeNew.getMonth()+1)+timeNew.getDay();
             var timeS=dataOfPort.reserve.ResDate.split('-').join("");*/




        });
    }
    /*
     * 拒绝外卖,显示拒绝外卖添加理由的一页
     * */
    var refuseTakeOut= function(obj,fun){

        var     oId=obj.data('id');



        OrderingSystem.getOrderById(oId, function (data) {
            var addressLong=(document.documentElement.clientWidth-150)+"px";
            if(data.data[0].status=='pre'){
                fun();
                var dataOfPort=data.data[0];
                var cpList="";

                var request="";
                if(dataOfPort.request.length==0){

                }else{
                    request= dataOfPort.specialRequest;
                }

                $('#subcancelOrderPage').html('');

                $.each(data.data[0].itemList,function(k,v){
                    cpList+=v.name+v.count+"份、";
                })

                $('<div class="cont">\
               <div class="main">\
                <dl class="orderTitle">\
                    <div class="listImg"></div>\
                    <dt>外卖</dt>\
                </dl>\
                <dl class="personInfo">\
                <div class="listHead">联系方式</div>\
                <pre>姓名：<br>\
电话：'+dataOfPort.mobile+'<br>\
<p style="width: '+addressLong+';"> 地址：'+dataOfPort.targetLabel+'<br></p>\
            </pre>\
            <a href="tel:'+dataOfPort.mobile+'"></a>\
            </dl>\
            <dl class="severCont">\
                <p class="go" data-function="btn_goToTakeOutOrderInfo" data-id="'+dataOfPort.id+'">〉</p>\
                <div class="sever">服务内容</div>\
                <pre>预定外卖<p class="no">券</p><br>\
菜品：'+cpList+'<br>\
特殊需求：<span class="demand">'+request+'</span><br>\
                </pre>\
            </dl>\
            <dl class="menuChose">\
                <div>支付状态</div>\
                <p>货到付款</p>\
                <h6>￥'+dataOfPort.total+'</h6>\
                </dl>\
                <dl class="refuseReason">\
                    <div>拒绝理由</div>\
                    <textarea cols="40" rows="4" class="take-out-refuse-reason"></textarea>\
                </dl>\
                </div>\
                </div>').appendTo('#subcancelOrderPage')
                $('<div class="bottom">\
                <div data-function="btn_refuseTakeOut" data-id="'+dataOfPort.id+'">确认拒绝任务</div>\
                </div>').appendTo('#subcancelOrderPage');
                /*         var timeNew=new Date();
                 timeNew=timeNew.getFullYear()+(timeNew.getMonth()+1)+timeNew.getDay();
                 var timeS=dataOfPort.reserve.ResDate.split('-').join("");*/
            }else{
                $('.modalOrderIsAccepted').show();
                $('.wait_gear').hide();
            }





        });
    }
    /*
     * 拒绝添加理由
     * */
    var refuseTakeOutAddReason=function(obj,fun){
        var options = {};
        options.orderId =obj.data('id');
        options.rejectReason = $(".take-out-refuse-reason").val();
        options.successFunction = function (data) {
            if(data.error>0){
                if(data.error=="33095"){
                    $('.modalOrderIsAccepted').show();
                    $('.wait_gear').hide();
                }else{
                    alert('请填写拒绝理由')
                    $('.wait_gear').hide();
                }


            }else{
                takeOutList(fun);
            }


        };
        OrderingSystem.rejectOrder(options);
    }
    /*
     * 接受外卖
     * */
    var acceptTakeOut = function(obj,fun){
        var options = {};
        options.OrderId=obj.data('id');
        options.successFunction = function (data) {
            if(data.error>0){
                $('.modalOrderIsAccepted').show();
                $('.wait_gear').hide();
            }else{
                var options = {};
                options.orderId = obj.data('id');
                options.successFunction = function (data) {

                    takeOutList(fun);

                };
                OrderingSystem.aceeptOrder(options);
            }

        };
        OrderingSystem.catchOrderByUid(options);

    }
    /*
     * 显示或者隐藏等待页面
     * */
    var waitPage = {
        closeflag:null,
        /*显示等待时间*/showWaitTime : function(){
            $(".wait_gear").fadeIn(100);
            var i=0;
            this.closeflag= setInterval(function(){
                i++;
                if(i==1){
                    $('.gear').css('background-position','0 -104px')
                }else if(i==2){
                    $('.gear').css('background-position','0 -181px')

                }else if(i==3){
                    $('.gear').css('background-position','0 -250px')
                    i=0;
                }else if(i==4){
                    $('.gear').css('background-position','0 -316px')
                    i=0;
                }

            },100)
        },
        /*关闭等待时间*/closeWaitTime :function(){
            $(".wait_gear").fadeOut(100);
            clearInterval(this.closeflag);
        }
    }
    /*
     * 添加外卖
     * */
    var addTakeOut = function(fun,addCpfun){
        waitPage.showWaitTime();

        var options = {};
        options.name = $(".newTakeoutOrder_name").val();
        options.sex = '1';
        options.mobile = $(".newTakeoutOrder_mobile").val();
        options.takeawayAddressCity = "";
        options.takeawayAddressSuburb = "";
        options.takeawayAddressAddress = $(".newTakeoutOrder_takeawayAddressAddress").val();
        options.itemList = [];
        options.successFunction = function (data) {

            if(data.error>0){
                alert('请补全信息')
                waitPage.closeWaitTime();
            }else{
                var orderId = data.data[0].id;
                takeOutList(fun);
                //alert(data.data[0].id)
                addCpfun(data.data[0].id,function(){
                    $(".newTakeoutOrder_name").val('');
                    $(".newTakeoutOrder_mobile").val('');
                    $(".newTakeoutOrder_takeawayAddressAddress").val('');

                    var optionsS= {};
                    optionsS.orderId = orderId;
                    optionsS.commonRequest = [];
                    optionsS.specialRequest = $.trim($('#submenuPage .specialDemand').val());

                    optionsS.successFunction = function (data) {
                        var oId = orderId;

                        var token = [];
                        var memberId = '';

                        var type = "cash";
                        OrderingSystem.getOrderById(oId, function (data) {
                            OrderingSystem.addOrderPayTrans(oId, data.data[0].total, type, function (data) {

                                var oIdP = orderId;
                                OrderingSystem.orderLineItemPrinter(oIdP, function (data) {
                                    OrderingSystem.bill(oId, token, memberId, function (data) {

                                    });//结账
                                });//打印
                            });
                        });



                    };
                    OrderingSystem.setOrderRequest(optionsS);//设置口味
                });


            }
        };
        OrderingSystem.newTakeoutOrder(options);
    }
    return{
        /*
         * 外卖列表
         * */
        takeOutList:function(fun){
            takeOutList(fun);
        },
        /*
         * 已接受外卖详情
         * */
        acceptedTakeOutInfo:function(obj,fun){
            acceptedTakeOutInfo(obj,fun)
        },
        /*
         * 未接受的外卖详情
         * */
        notAcceptedTakeOutInfo:function(obj,fun){
            notAcceptedTakeOutInfo(obj,fun);
        },
        /*
         * 拒绝外卖,显示拒绝外卖添加理由的一页
         * */
        refuseTakeOut:function(obj,fun){
            refuseTakeOut(obj,fun);
        },
        /*
         * 拒绝添加理由
         * */
        refuseTakeOutAddReason:function(obj,fun){
            refuseTakeOutAddReason(obj,fun);
        },
        /*
         * 接受外卖
         * */
        acceptTakeOut:function(obj,fun){
            acceptTakeOut(obj,fun);
        },
        /*
         * 添加外卖
         * */
        addTakeOut:function(fun,addCpfun){
            addTakeOut(fun,addCpfun);
        }
    }
}();